<template>
    <div>
        <el-card class="mb">
            <el-input placeholder="请输入关键词搜索" v-model="queryform.keyword"
                style="width: 200px;margin-right: 10px;"></el-input>
            <el-button type="primary" @click="search()" plain>查询</el-button>
            <el-button type="info" @click="research()" plain>重置</el-button>
            <el-button type="warning" style="float:right" @click="add()" plain>新增</el-button>
        </el-card>
        <el-card>
            <div class="table">
                <el-table :data="tableData" strip>

                    <el-table-column type="index" label="序号" align="center" sortable></el-table-column>
                    <el-table-column prop="name" label="名称"></el-table-column>
                    <el-table-column prop="img" label="图片" align="center">
                        <template v-slot="scope">
                            <div style="display: flex;justify-content: center; align-items: center">
                                <el-image style="width: 40px; height: 40px; border-radius: 50%" v-if="scope.row.icon"
                                    :src="scope.row.icon" :preview-src-list="[scope.row.icon]"></el-image>
                            </div>
                        </template>

                    </el-table-column>

                    <el-table-column prop="origin" label="原料"></el-table-column>
                    <el-table-column prop="intro" label="简介"></el-table-column>
                    <el-table-column prop="typeName" label="分类"></el-table-column>
                    <el-table-column prop="price" label="价格"></el-table-column>
                    <el-table-column prop="buyNum" label="销量"></el-table-column>
                    <el-table-column prop="watchNum" label="浏览量"></el-table-column>

                    <el-table-column prop="status" label="状态">
                        <template v-slot="scope">
                            <el-tag v-if="scope.row.status == '上架'" type="success">{{ scope.row.status }}</el-tag>
                            <el-tag v-if="scope.row.status == '下架'" type="danger">{{ scope.row.status }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="240" fixed="right">
                        <template v-slot="scope">
                            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
                            <el-button size="mini" type="primary" plain @click="addorxiaj(scope.row)">{{ scope.row.status
                                == '上架' ? '下架':'上架'}}</el-button>
                            <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <div class="pagination" style="margin-top: 10px;">
                    <el-pagination background @current-change="handleCurrentChange" :current-page="queryform.pageNum"
                        :page-sizes="[5, 10, 20]" :page-size="queryform.pageSize" layout="total, prev, pager, next"
                        :total="total">
                    </el-pagination>
                </div>
            </div>

        </el-card>
        <el-dialog title="咖啡信息" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
            <el-form :model="form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="formRef">
                <el-form-item label="图片" prop="icon">
                    <el-upload class="avatar-uploader" :file-list="fileList" action="/files/upload"
                        :headers="uploadHeaders" :show-file-list="false" :on-success="handleAvatarSuccess">
                        <img v-if="form.icon" :src="form.icon" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="分类名称" prop="typeId">
                    <el-select v-model="form.typeId" placeholder="名称" style="width: 100%;">
                        <el-option v-for="item in typelist" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="原料" prop="origin">
                    <el-input v-model="form.origin" placeholder="请输入原料"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model="form.price" placeholder="请输入价格"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="intro">
                    <el-input v-model="form.intro" placeholder="请输入描述"></el-input>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="fromVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import service from '@/request';

export default {
    data() {
        return {
            tableData: [],  // 所有的数据
            pageNum: 1,   // 当前的页码
            pageSize: 10,  // 每页显示的个数
            total: 0,
            username: null,
            img: "",
            token: "",
            typelist: [],
            fileList: [],
            name: null,
            uploadHeaders: {

                Authorization: window.localStorage.getItem("token")
            },

            fromVisible: false,
            avatar: "",
            queryform: {
                keyword: "",
                pageNum: 1,
                pageSize: 10
            },
            license: "",
            form: {
                img: "",

            },
            user: {},
            rules: {

                icon: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                ],
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                ],
                remark: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                ],

                // business_id: [
                //     { required: true, message: '请选择商家类型', trigger: 'blur' },
                // ]
            },
            ids: [],
            sjlist: [],
        }
    },
    mounted() {
        this.token = window.localStorage.getItem("token")
        this.user = JSON.parse(window.localStorage.getItem("userinfo"))
        console.log(this.user)

        this.getlist()
        this.getall()
    },
    methods: {
        addorxiaj(row) {
            this.$confirm('是否确认上下架?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const res = await service.get("/info/uporoff?id=" + row.id);
                this.getlist()
            })
        },
        async getall() {
            const res = await service.get("/type/all");
            console.log(res)
            this.typelist = res.data
        },


        del(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const res = await service.get("/info/delete?id=" + id)
                console.log(res)
                if (res.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getlist()
                }

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        force(e) {
            this.form.timeRange = e;
            this.$forceUpdate();
        },
        handleEdit(row) {
            this.form = row;
            this.img = row.img;


            this.fromVisible = true
        },
        search() {
            this.queryform.pageNum = 1;
            this.queryform.pageSize = 10;
            this.getlist()
        },
        research() {
            this.queryform.keyword = ""
            this.queryform.pageNum = 1;
            this.queryform.pageSize = 10;
            this.getlist()

        },
        async getlist() {
            // if (this.user.role == 'HOTEL') {
            //     this.queryform.hotelId = this.user.id

            // }else{
            //     this.queryform.hotelId = ""

            // }
            const res = await service.get("/info/list", { params: this.queryform });
            console.log(res);
            if (res.code == 200) {
                this.tableData = res.data.records;
                this.total = res.data.total;
            }


        },
        add() {
            this.img = ""
            this.form = {}

            this.fromVisible = true

        },
        handleLicenseSuccess(e) {

        },
        async save() {

            const res = await service.post("/info/addorupdate", this.form);
            if (res.code == 200) {
                this.fromVisible = false
                this.$message.success("操作成功")
                this.getlist()
            }





        },
        handleCurrentChange(e) {

        },
        handleAvatarSuccess(e) {
            console.log(e)

            this.form.icon = e.data;
            this.$forceUpdate()
            // this.fileList.push({

            // })

        },

    }
}
</script>
<style scoped>
.mb {
    margin-bottom: 10px;
}
</style>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px !important;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>